#include <bits/stdc++.h>

using namespace std;

constexpr int N = 1e6 + 5;

int heap[N], len = 0;

void push(int e){
    heap[++len] = e;
    
    int i = len;
    while (i > 1 && heap[i] < heap[i/2]){
        swap(heap[i], heap[i/2]);
        i /= 2;
    }
}

void pop(){
    heap[1] = heap[len--];

    int i = 1;
    while (2 * i <= len){
        int lson = 2 * i;
        if (lson < len && heap[lson] > heap[lson + 1]){
            ++lson;
        }
        if (heap[i] > heap[lson]){
            swap(heap[i], heap[lson]);
            i = lson;
        }
        else{
            break;
        }
    }
}

int main(){
    int n;
    cin >> n;

    while (n--){
        int op;
        cin >> op;
        
        if (op == 1){
            int e;
            cin >> e;
            push(e);
        }
        else if (op == 2){
            cout << heap[1] << endl;
        }
        else{
            pop();
        }
    }

    return 0;
}